﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;

namespace ADB_QuanLyThuVien
{
    public class DAO_Books
    {
        private static string GetNextId() // Tạo mã sách
        {
            string id = "BO";
            DateTime now = DateTime.Now;
            id += now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00");
            return id;
        }

        private static bool isExist(Book sach) //Kiểm tra sách tồn tại
        {
            bool kq = false;
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                Book b = new Book();
                b.Namxb = sach.Namxb;
                b.TenSach = sach.TenSach;
                IObjectSet result = db.QueryByExample(b);
                if (result.Count > 0)
                    kq = true;
            }
            return kq;
        }

        public static List<Book> SelectAll() //Lấy tất cả các sách
        {
            List<Book> list = new List<Book>();
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                Book sach = new Book();
                IObjectSet result = db.QueryByExample(sach);
                for (int i = 0; i < result.Count; ++i)
                    list.Add((Book)result[i]);
                //db.Close();
            }
            return list;
        }

        public static bool RemoveBook(string idBook) //Xóa sách
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            Book sach = new Book();
            sach.MaSach= idBook;
            IObjectSet result = db.QueryByExample(sach);
            db.Delete((Book)result[0]);
            db.Close();
            return kq;
        }

        public static bool AddBook(Book sach, string idAu, string idCa, string idPu, string idEm) //Thêm sách
        {
            bool kq = true;
            if (isExist(sach) == true)
                kq = false;
            else
            {
                var config = Db4oEmbedded.NewConfiguration();
                config.Common.ObjectClass(typeof(Book)).CascadeOnUpdate(true);
                IObjectContainer db = Db4oEmbedded.OpenFile(config, QLTV_System.ConnectString);
                sach.MaSach = GetNextId();
                //Trỏ đến đối tượng tác giả
                IObjectSet rAuthor = db.QueryByExample(new Author(idAu, null, null, new DateTime(), null, null, null, null, null));
                sach.Tacgia = (Author)rAuthor.Next();
                //Trỏ đến đối tượng thể loại
                IObjectSet rCategory = db.QueryByExample(new Category(idCa,null));
                sach.TheLoai = (Category)rCategory.Next();
                //Trỏ đến đối tượng nhà xuất bản
                IObjectSet rPublisher = db.QueryByExample(new DTO_NhaXuatBan(idPu,null,null,null,null));
                sach.NhaXB = (DTO_NhaXuatBan)rPublisher.Next();
                //Trỏ đến đối tượng nhân viên
                Member emp = new Member();
                emp.Id = idEm;
                IObjectSet rEmployee = db.QueryByExample(emp);
                sach.NhanVien = (Member)rEmployee.Next();
                db.Store(sach);
                db.Close();
            }
            return kq;
        }

        public static List<Book> SelectBooks(Book sach, string tenSach) //Lấy tất cả các sách
        {
            List<Book> list = new List<Book>();
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                IObjectSet lBook = db.QueryByExample(sach);
                for (int i = 0; i < lBook.Count; ++i)
                    if (tenSach == null)
                        list.Add((Book)lBook[i]);
                    else
                    {
                        Book s = (Book)lBook[i];
                        if (s.TenSach.ToUpper().Contains(tenSach.ToUpper()))
                            list.Add(s);
                    }
            }
            return list;
        }

        
    }
}
